﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CarNCDDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarNCDDB
    {
        public CarNCDDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarNCD CarNCD)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarNCDInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderNCDId", CarNCD.ProviderNCDId);
            _sPara.Add("@ProviderCompanyId", CarNCD.ProviderCompanyId);
            _sPara.Add("@NCD", CarNCD.NCD);
            _sPara.Add("@Rate", CarNCD.Rate);
            _sPara.Add("@RefCEUserId", CarNCD.RefCEUserId);
            _sPara.Add("@Status", CarNCD.Status);
            _sPara.Add("@Ninfo1", CarNCD.NInfo1);
            _sPara.Add("@Finfo1", CarNCD.FInfo1);
            _sPara.Add("@Dinfo1", CarNCD.DInfo1);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public CarNCD GetProviderCarNCD(Int64 ProviderNCDId)
        {
            CarNCD _carncd = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarNCDSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderNCDId", ProviderNCDId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carncd = new CarNCD();
                    _carncd = FillDataRecord(_dt, 0);
                }
            }
            return _carncd;
        }

        public CarNCD GetProviderCarNCD(Int64 ProviderCompanyId, string NCD)
        {
            CarNCD _carncd = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarNCDSelectSingleItemSelected]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@NCD", NCD);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carncd = new CarNCD();
                    _carncd = FillDataRecord(_dt, 0);
                }
            }
            return _carncd;
        }

        public CarNCD GetProviderCarNCD(Int64 ProviderCompanyId, Int64 CoverType)
        {
            CarNCD _carncd = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarNCDCoverTypeSelectSingleItemSelected]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@Ninfo1", CoverType);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carncd = new CarNCD();
                    _carncd = FillDataRecord(_dt, 0);
                }
            }
            return _carncd;
        }

        private CarNCD FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarNCD myCarNCD = new CarNCD();
            myCarNCD.ProviderNCDId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderNCDId"].ToString());
            myCarNCD.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarNCD.NCD = myDataTable.Rows[RowId]["NCD"].ToString();
            myCarNCD.Rate = Convert.ToSingle(myDataTable.Rows[RowId]["Rate"].ToString());
            myCarNCD.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarNCD.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myCarNCD.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            if (myDataTable.Rows[RowId]["NInfo1"].ToString() != "")
                myCarNCD.NInfo1 = Convert.ToInt64(myDataTable.Rows[RowId]["NInfo1"].ToString());

            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarNCD.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarNCD.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarNCD.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            return myCarNCD;
        }
    }

}